package com.health.db.dao.order.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONArray;
import com.health.db.dao.ServiceInterface;
import com.health.db.dao.order.IRegisterOrderDao;
import com.health.db.mapper.RegisterOrderMapper;
import com.health.db.model.RegisterOrder;
import com.health.db.model.RegisterOrderExample;
import com.health.mapp.utils.Constant;
import com.health.redis.RedisConstants;


public class RegisterOrderDaoImpl extends ServiceInterface implements IRegisterOrderDao{

	RegisterOrderMapper registerOrderMapper;
	String  method=":method:";
	String  stuffId=":docId:";
	String  userId=":userId:";
	String  orderId=":orderId";
	String  detailOrderId=":dOrderId:";
	String  refbillid = ":billId:";
	String  state = ":state:";
	String  type=":type:";
	String  channel=":channel:";
	String  offset=":OF:";
	String  pageSize=":PS:";
	@Override
	public List<RegisterOrder> selectSimple(RegisterOrderExample example) {
		// TODO Auto-generated method stub
		 
		List<RegisterOrder>  registerOrderList =null;
		String appId= example.getAppId();
		if (StringUtils.isNotBlank(appId)&&appId.equals(Constant.PARTNER_APPID)) {
			registerOrderList  = this.selectSimpleWithMSG(example);
		}else{
			registerOrderList  = this.selectSimpleWithNoMSG(example);
		}
		return registerOrderList;
	}
	
	public List<RegisterOrder> selectSimpleWithMSG(RegisterOrderExample example) {
		// TODO Auto-generated method stub
		
		List<RegisterOrder>  list =null;
		String key = RedisConstants.RegisterOrderMapper+"M";
		String keys = "";
		
		if (null!=example.getRegisterid()) {
			key=key+detailOrderId+example.getRegisterid();
		}else if(null!=example.getUserid()){
			key=key+userId+example.getUserid()+offset+example.getOffset()+pageSize+example.getPageSize();
			keys=RedisConstants.RegisterOrderMapper+userId+example.getUserid();
		}else if (null!=example.getAccepterid()) {
			key=key+stuffId+example.getAccepterid()+offset+example.getOffset()+pageSize+example.getPageSize();
			keys=RedisConstants.RegisterOrderMapper+stuffId+example.getAccepterid();
		}else if (null!=example.getRefbillid()) {
			key=key+refbillid+example.getRefbillid();
		}else{
			
			return null;
		}
		
		if (null!=example.getRunstatusid()) {
			key = key+state+example.getRunstatusid();
		}else if (null!=example.getRunstatusids()) {
			key = key+state+JSONArray.toJSONString(example.getRunstatusid());
		}
		
		if (null!=example.getRegistertypeid()) {
			key = key+type+example.getRegistertypeid();
		}
		
		if (null!=example.getRegisterguideid()) {
			key = key+channel+example.getRegisterguideid();
		}
		getRedisOperatiorLogic();
		 Object object=null;
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
			}
			
			if(null!=object )
			{
				if (!"".equals(object)) {
					list=(List<RegisterOrder> ) object;
				}
			}
			else
			{
				list = registerOrderMapper.selectSimpleWithMsg(example);
				
				if(redisOperatiorLogic.isJedisIsConnected())
				{
					redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
					if (StringUtils.isNoneBlank(keys)) {
						redisOperatiorLogic.lpush(keys, key, getOrderRedisGroupNo("1"));
					}
				}
			}
		
		return list;
	}
	
	public List<RegisterOrder> selectSimpleWithNoMSG(RegisterOrderExample example) {
		// TODO Auto-generated method stub
		 
		List<RegisterOrder>  list =null;
		String key = RedisConstants.RegisterOrderMapper;
		String keys = "";
		
		if (null!=example.getRegisterid()) {
			key=key+detailOrderId+example.getRegisterid();
		}else if(null!=example.getUserid()){
			key=key+userId+example.getUserid()+offset+example.getOffset()+pageSize+example.getPageSize();
			keys=RedisConstants.RegisterOrderMapper+userId+example.getUserid();
		}else if (null!=example.getAccepterid()) {
			key=key+stuffId+example.getAccepterid()+offset+example.getOffset()+pageSize+example.getPageSize();
			keys=RedisConstants.RegisterOrderMapper+stuffId+example.getAccepterid();
		}else if (null!=example.getRefbillid()) {
			key=key+refbillid+example.getRefbillid();
		}else{
			
			return null;
		}
		
		if (null!=example.getRunstatusid()) {
			key = key+state+example.getRunstatusid();
		}else if (null!=example.getRunstatusids()) {
			key = key+state+JSONArray.toJSONString(example.getRunstatusid());
		}
		
		if (null!=example.getRegistertypeid()) {
			key = key+type+example.getRegistertypeid();
		}
		
		if (null!=example.getRegisterguideid()) {
			key = key+channel+example.getRegisterguideid();
		}
		getRedisOperatiorLogic();
		Object object=null;
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
			}
			if(null!=object )
			{
				if (!"".equals(object)) {
					list=(List<RegisterOrder> ) object;
				}
			}
			else
			{
				list = registerOrderMapper.selectSimple(example);
				
				if(redisOperatiorLogic.isJedisIsConnected())
				{
					redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
					if (StringUtils.isNoneBlank(keys)) {
						redisOperatiorLogic.lpush(keys, key, getOrderRedisGroupNo("1"));
					}
				}
			}
		return list;
	}
	
	
	
	
	@Override
	public int insert(RegisterOrder record) {
		// TODO Auto-generated method stub
		 
		int result=-1;
		result=registerOrderMapper.insert(record);
		getRedisOperatiorLogic();
			
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+userId+record.getUserid(),  getOrderRedisGroupNo("1"));

			if (null!=record.getAccepterid()) {
				redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+stuffId+record.getAccepterid(),  getOrderRedisGroupNo("1"));
				
			}
		}
		
		
		return result;
	}

	@Override
	public int updateOrderSelective(RegisterOrder record) {
		int result=-1;

		result=registerOrderMapper.updateOrderSelective(record);
		deleteCach(record);
		return result;
	}
	
	@Override
	public int updateOrderForQuit(RegisterOrder record){
		int result=-1;

		result=registerOrderMapper.updateOrderForQuit(record);
		deleteCach(record);
		getRedisOperatiorLogic();
		if (null!=record.getAccepterid()) {
				redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+stuffId+record.getAccepterid(),  getOrderRedisGroupNo("1"));
	    }
		return result;
		
	}

	
    
   //删除缓存
   private void deleteCach( RegisterOrder record){
	   
	    getRedisOperatiorLogic();
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			redisOperatiorLogic.deleteObject(RedisConstants.RegisterOrderMapper+orderId+record.getRegisterid(),  getOrderRedisGroupNo("1"));
			redisOperatiorLogic.deleteObject(RedisConstants.RegisterOrderMapper+detailOrderId+record.getRegisterid(),  getOrderRedisGroupNo("1"));
			RegisterOrder curr = this.selectSimpleSingleByOrderId(record.getRegisterid());
			redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+userId+curr.getUserid(),  getOrderRedisGroupNo("1"));
			if (null!=curr.getAccepterid()) {
				redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+stuffId+curr.getAccepterid(),  getOrderRedisGroupNo("1"));
			}
			
			if (StringUtils.isNotEmpty(curr.getSsMeasure())) {
				redisOperatiorLogic.deleteKeys(RedisConstants.RegisterOrderMapper+stuffId+curr.getSsMeasure(),  getOrderRedisGroupNo("1"));
			}
			
		}
   }

	@Override
	public RegisterOrder  selectSimpleSingleByOrderId(Long registerid) {
		// TODO Auto-generated method stub
		
		 getRedisOperatiorLogic();
		
		 String key=RedisConstants.RegisterOrderMapper+orderId+registerid;
		
		 RegisterOrder registerOrderTemp =null;
		 Object object=null;
	
				if(redisOperatiorLogic.isJedisIsConnected())
				{
					object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
				}
				
				if(null!=object )
				{
					if (!"".equals(object)) {
						registerOrderTemp=(RegisterOrder ) object;
					}
				}
				else
				{
					registerOrderTemp =  registerOrderMapper.selectSimpleSingleByOrderId(registerid);
					
					if(redisOperatiorLogic.isJedisIsConnected())
					{
						redisOperatiorLogic.setEx(key, registerOrderTemp, getOrderRedisGroupNo("1"));
					}
				}
		 return registerOrderTemp;
	}

	@Override
	public int updateOrderState4Timeout(RegisterOrder record) {
		int result=-1;
			result=registerOrderMapper.updateOrderState4Timeout(record);
		return result;
	}

	@Override
	public List<RegisterOrder> selectOrderState4Timeout(
			RegisterOrderExample example) {
		// TODO Auto-generated method stub
		return registerOrderMapper.selectOrderState4Timeout(example);
	}

	@Override
	public List<RegisterOrder> selectSimpleByEvaluate(Long docId) {
		
		getRedisOperatiorLogic();
		
		String key=RedisConstants.RegisterOrderMapper+method+"eval"+stuffId+docId;
		
		List<RegisterOrder>  list=null;
		 Object object=null;
	
				if(redisOperatiorLogic.isJedisIsConnected())
				{
					object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
				}
				
				if(null!=object )
				{
					if (!"".equals(object)) {
						list=(List<RegisterOrder> ) object;
					}
				}
				else
				{
					list = registerOrderMapper.selectSimpleByEvaluate(docId);
					
					if(redisOperatiorLogic.isJedisIsConnected())
					{
						redisOperatiorLogic.setEx(key, list, getOrderRedisGroupNo("1"));
					}
				}
		return list;
	}
	
	@Override
	public List<RegisterOrder> selectSimpleOrderGrapPool(RegisterOrderExample example) {
		// TODO Auto-generated method stub
		 
		String garpStr = JSONArray.toJSONString(example.getCommonOrderIds());
		getRedisOperatiorLogic();
		
		String key=RedisConstants.RegisterOrderMapper+garpStr;
		
		List<RegisterOrder>  list=null;
		Object object=null;
	
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		
		if(null!=object )
		{
			if (!"".equals(object)) {
				list=(List<RegisterOrder> ) object;
			}
		}
		else
		{
			list = registerOrderMapper.selectSimpleOrderGrapPool(example);
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				redisOperatiorLogic.setEx(key, 1,list, getOrderRedisGroupNo("1"));
			}
		}
		return list;
	}
	
	
	

	@Override
	public boolean orderAcceptLocked(Long registerid, int type) {
		// TODO Auto-generated method stub
		getRedisOperatiorLogic();
		String key=RedisConstants.RegisterOrderMapper+":lock:"+registerid;
		//锁定
		if (type==0) {
	
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			if (redisOperatiorLogic.exists(key, getOrderRedisGroupNo("1"))) {
				return  false;
			}else{
				redisOperatiorLogic.setEx(key,key, getOrderRedisGroupNo("1"));
				return true;
			}
		}else {
			return true;
		}
		//解锁
		}else{
			redisOperatiorLogic.deleteObject(key, getOrderRedisGroupNo("1"));
			return true;
		}

	}

/*	@Override
	public RegisterOrder selectSimpleSingleByOrderIdForUpdate(Long registerid) {
		// TODO Auto-generated method stub
		
		if (null==registerid||0==registerid.intValue()) {
			return null;
		}else{
			RegisterOrder  registerOrderTemp=null;
			registerOrderTemp = registerOrderMapper.selectSimpleSingleByOrderIdForUpdate(registerid);
	 
			return registerOrderTemp;
			
		}
	}
	*/
	
	/******************************************************************************************************************************/
	public RegisterOrderMapper getRegisterOrderMapper() {
		return registerOrderMapper;
	}

	public void setRegisterOrderMapper(RegisterOrderMapper registerOrderMapper) {
		this.registerOrderMapper = registerOrderMapper;
	}

}
